package com.zhenxiang.juc.lock.lock8;

import java.util.concurrent.TimeUnit;

/**
 * @Author : James
 * @Create : 2021-12-21-15:46
 * @Description :
 */
public class Test4 {

    public static void main(String[] args) {
        Phone4 phone1= new Phone4();
        Phone4 phone2 =new Phone4();


        new Thread(()->{
            phone1.sendSms();
        },"A").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            phone2.call();
        },"B").start();
    }
}

// 资源类
class Phone4{

    /**
     * synchronized 锁的是对象的调用者
     * static 静态方法 类一加载就调用
     * synchronized 对于static 锁的是Class
     */
    public static synchronized void sendSms() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public  synchronized void call(){
        System.out.println("打电话");
    }


}
